<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_COMPONENT_CONTROLLER', 'true');

require_once('includes/components/CaptchaComponent.php');

class ContactUsComponent
{
	function ContactUsComponent()
	{
		$this->process_code = 0;
	}

	function action(&$params, $action)
	{
		if ($action == 'Submit') 
			return $this->submit($params);
		else
			return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$value{$handle}{0}{'process_code'}= $this->process_code;
		return $value;
	}

	function submit(&$params)
	{
		$accountId = (int)$params['accountId'];

		if ($accountId < 1) {
			//CAPTCHA
			$isCaptchaCodeValid = validateCaptchaCode($params);
			if ($isCaptchaCodeValid < 0) {$this->process_code = $isCaptchaCodeValid; return $this->query($params);}
		}

		$display_company = returnBoolean(ACCOUNT_COMPANY);
		$display_gender = returnBoolean(ACCOUNT_GENDER);
		$display_state = returnBoolean(ACCOUNT_STATE);
		$display_suburb = returnBoolean(ACCOUNT_SUBURB);

		$entry_firstname_length_min = (int)ENTRY_FIRST_NAME_MIN_LENGTH;
		$entry_lastname_length_min = (int)ENTRY_LAST_NAME_MIN_LENGTH;
		$entry_email_address_length_min = (int)ENTRY_EMAIL_ADDRESS_MIN_LENGTH;
		$entry_email_address_format_check =returnBoolean(ENTRY_EMAIL_ADDRESS_FORMAT_CHECK);
		$entry_street_address_length_min = (int)ENTRY_STREET_ADDRESS_MIN_LENGTH;
		$entry_city_length_min = (int)ENTRY_CITY_MIN_LENGTH;
		$entry_state_length_min = (int)ENTRY_STATE_MIN_LENGTH;
		$entry_postcode_length_min = (int)ENTRY_POSTCODE_MIN_LENGTH;
		$entry_telephone_length_min = (int)ENTRY_TELEPHONE_MIN_LENGTH;

		$addressId = (int)$params['addressId'];
		$subject = (int)$params['subject'];
		$comment = prepare_input($params['comment']);

		if ($subject == null) $subject = '';
		if ($comment == null) $comment = '';

		$required_fields_available = true;

		if ($accountId > 0) {

			$sqlQuery = 'select customers_gender, customers_firstname, customers_lastname, customers_email_address, ' .
			'customers_telephone, customers_isWholesale, customers_isLogin, customers_default_address_id ' .
			'from ' . TABLE_CUSTOMERS . ' where customers_id=\'' . $accountId . '\'';
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$i = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$gender = $row['customers_gender'];
				$firstname = $row['customers_firstname'];
				$lastname = $row['customers_lastname'];
				$email = $row['customers_email_address'];
				$phone = $row['customers_telephone'];
				$default_addressId = (int)$row['customers_default_address_id'];
				
			}
			if ($addressId < 1) $addressId = $default_addressId;
			$addresses = getAddresses($accountId, $default_addressId);
			reset($addresses);
			while (list(,$address) = each($addresses))
			{
				if ($addressId == $address['id']) {$contactAddress = $address; break;}
			}
			$company = $contactAddress['company'];

			$street = $contactAddress['street_address'];
			$suburb = $contactAddress['suburb'];
			$city = $contactAddress['city'];
			$state = $contactAddress['state'];
			$postcode = $contactAddress['postcode'];
			$country = $contactAddress['country_iso_code_3'];
		}
		else {
			$gender = prepare_input($params['gender']);
			$firstname = prepare_input($params['firstname']);
			$lastname = prepare_input($params['lastname']);
			$email = prepare_input($params['email']);

			$street = prepare_input($params['street']);
			$suburb = prepare_input($params['suburb']);
			$city = prepare_input($params['city']);
			$state = prepare_input($params['state']);
			$postcode = prepare_input($params['postcode']);
			$country = prepare_input($params['country']);

			$phone = prepare_input($params['phone']);
			$company = prepare_input($params['company']);

			if ($gender == null) $gender = '';
			if ($firstname == null) $firstname = '';
			if ($lastname == null) $lastname = '';
			if ($email == null) $email = '';

			if ($street == null) $street = '';
			if ($suburb == null) $suburb = '';
			if ($city == null) $city = '';
			if ($state == null) $state = '';
			if ($postcode == null) $postcode = '';
			if ($country == null) $country = '';

			if ($phone == null) $phone = '';
			if ($company == null) $company = '';

			if ($required_fields_available && strlen($firstname) < $entry_firstname_length_min) $required_fields_available = false;
			if ($required_fields_available && strlen($lastname) < $entry_lastname_length_min) $required_fields_available = false;
			if ($required_fields_available && strlen($email) < $entry_email_address_length_min) $required_fields_available = false;
			if ($required_fields_available && strlen($street) < $entry_street_address_length_min) $required_fields_available = false;
			if ($required_fields_available && strlen($city) < $entry_city_length_min) $required_fields_available = false;
			if ($display_state && $required_fields_available && strlen($state) < $entry_state_length_min) $required_fields_available = false;
			if ($required_fields_available && strlen($postcode) < $entry_postcode_length_min) $required_fields_available = false;
			if ($required_fields_available && strlen($phone) < $entry_telephone_length_min) $required_fields_available = false;
		}

		$address = $street . ' ' . $city;
		if ($display_suburb && strlen($suburb) > 0) $address .= ', ' . $suburb;
		if ($display_state) $address .= ', ' . $state;
		$address .=  ' ' . $postcode . ' ' . $country;

		if ($required_fields_available && ($subject < 1 || $subject > 4)) $required_fields_available = false;
		
		if (!$required_fields_available) {$this->process_code = -5; return $this->query($params);}

		$subject_text = '';
		if ($subject == 1) $subject_text = CU_SUBJECT_1;
		else if ($subject == 2) $subject_text = CU_SUBJECT_2;
		else if ($subject == 3) $subject_text = CU_SUBJECT_3;
		else if ($subject == 4) $subject_text = CU_SUBJECT_4;

		if ($subject_text == '') {$this->process_code = -6; return $this->query($params);}
		
		if (!validate_email_format($email, true)) {$this->process_code = -7; return $this->query($params);}
		
		$email_subject = CU_EMAIL_SUBJECT;
		$pattern = '/search engine|your web traffic|your online leads|1st page of Google/i';
		if (preg_match($pattern, $comment)) $email_subject = CU_SPAM . ' ' . CU_EMAIL_SUBJECT;

		$name = $firstname . ' ' . $lastname;

		$text = sprintf(CU_EMAIL_BODY_TEMPLATE, $name, $name, $email, $phone, $company, $address, $subject_text, $comment);
		send_email(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, $email_subject, $text, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);

		$this->process_code = 1;
		return $this->query($params);
	}
}
?>
